﻿using NHibernate.Cfg;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;

namespace nsadv.Statistic.Repository.NHibernate.Config
{
	public static class ConfigProvider
	{
		private static IPersistenceConfigurer GetDbConfig(DbDriver dbDriver, string connectionString)
		{
			switch (dbDriver)
			{
				case DbDriver.SQLServer:
					return MsSqlConfiguration
						.MsSql2008
						.ConnectionString(connectionString)
						.Raw("command_timeout", "300")
						.Cache(x => x
							.UseQueryCache()
							.UseMinimalPuts()
							.ProviderClass<global::NHibernate.Caches.SysCache2.SysCacheProvider>())
						.UseReflectionOptimizer();

				case DbDriver.SQLCompact:
					return MsSqlCeConfiguration
						.Standard
						.ConnectionString(connectionString)
						.Raw("command_timeout", "300")
						.Cache(x => x
							.UseQueryCache()
							.UseMinimalPuts()
							.ProviderClass<global::NHibernate.Caches.SysCache2.SysCacheProvider>())
						.UseReflectionOptimizer();
			}
			return null;
		}

		public static Configuration GetConfig(
			DbDriver dbDriver,
			string connectionString)
		{
			return Fluently.Configure()
				.Database(GetDbConfig(dbDriver, connectionString))
				.Mappings(x => x
							.FluentMappings
								.Add<Maps.UserMap>()
								.Add<Maps.BannerMap>()
								.Add<Maps.AdvertiserMap>()
								.Add<Maps.PlaceMap>()
								.Add<Maps.PlatformMap>()
								.Add<Maps.CampaignMap>()
								.Add<Maps.StatisticItemMap>())
				.BuildConfiguration();
		}
	}
}
